home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Format 1994 October
/
Macformat17.cdr
/
Shareware City
/
Developers
/
shutdown-fx-20-c
/
sfx init ƒ
/
code ƒ
/
sfx install.c
< prev
next >
Wrap
Text File
|
1994-07-11
|
4KB
|
119 lines
/**********************************************************************\
File: sfx install.c
Purpose: This module handles installing shutdown procs and installing
and removing fades from the system heap.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program in a file named "GNU General Public License".
If not, write to the Free Software Foundation, 675 Mass Ave,
Cambridge, MA 02139, USA.
\**********************************************************************/
#include "Shutdown.h"
#include "sfx install.h"
#include "sfx gestalt.h"
#include "program globals.h"
short InstallTheShutdownProc(Str255 theName, short vRefNum, long dirID,
Boolean onShutdown, Boolean onRestart)
/* assume: there are no shutdown procs currently installed */
/* assume: sfx Gestalt function is properly installed */
{
OSErr theResError;
FSSpec fs;
short oldRefNum, newRefNum;
Boolean alreadyOpen;
Handle theProcHandle;
ProcPtr theFade, theShutdownProc, theRestartProc;
unsigned long theSize;
unsigned long dummy;
short resultCode;
BlockMove(theName, fs.name, theName[0]+1);
fs.vRefNum=vRefNum;
fs.parID=dirID;
theResError=OpenTheResFile(&fs, &oldRefNum, &newRefNum, &alreadyOpen);
if (theResError!=noErr)
return kCantOpenModule;
theProcHandle=Get1Resource('PROC', 0);
if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
return kModuleDamaged;
if (*theProcHandle==0L)
LoadResource(theProcHandle);
if (*theProcHandle==0L)
return kModuleDamaged;
HLock(theProcHandle);
theFade=NewPtrSys(theSize=GetHandleSize(theProcHandle));
BlockMove(*theProcHandle, theFade, theSize);
ReleaseResource(theProcHandle);
theProcHandle=0L;
CloseTheResFile(oldRefNum, newRefNum, alreadyOpen);
theProcHandle=Get1Resource('PROC', 10); /* get shutdown proc from sfx init */
if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
return kCantGetProc10Resource;
if (*theProcHandle==0L)
LoadResource(theProcHandle);
if (*theProcHandle==0L)
{
DisposePtr(theFade);
return kCantGetProc10Resource;
}
theShutdownProc=NewPtrSys(theSize=GetHandleSize(theProcHandle));
theRestartProc=NewPtrSys(theSize);
BlockMove(*theProcHandle, theShutdownProc, theSize);
BlockMove(*theProcHandle, theRestartProc, theSize);
ReleaseResource(theProcHandle);
theProcHandle=0L;
*(ProcPtr*)((long)theShutdownProc+6)=
*(ProcPtr*)((long)theRestartProc+6)=theFade; /* store address of fade */
*(short*)((long)theShutdownProc+10)=onShutdown ? 1 : 0; /* store on shutdown flag */
*(short*)((long)theRestartProc+10)=onRestart ? 1 : 0; /* store on restart flag */
ShutDwnInstall(theShutdownProc, sdOnPowerOff);
ShutDwnInstall(theRestartProc, sdOnRestart);
resultCode=SetGestaltProcPtrs(theFade, theShutdownProc, theRestartProc);
return resultCode;
}
OSErr OpenTheResFile(FSSpec* fs, short* oldRefNum, short* newRefNum, short* alreadyOpen)
{
unsigned long oldTopMapHndl;
OSErr theResError;
*oldRefNum=CurResFile();
oldTopMapHndl=(unsigned long)TopMapHndl;
*newRefNum=HOpenResFile(fs->vRefNum, fs->parID, fs->name, fsRdPerm);
theResError=ResError();
*alreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
return theResError;
}
void CloseTheResFile(short oldRefNum, short newRefNum, Boolean alreadyOpen)
{
if (!alreadyOpen)
{
CloseResFile(newRefNum);
}
UseResFile(oldRefNum);
}